/*

	This file adds adds variables from other data sets to the cleaned Prodcom data

*/

cd ${work}2_prepare

use temp\vars_ipt, clear
destring jurnr, gen(jurnr_num)

*FIRM LEVEL CHARACTERISTICS
merge m:1 jurnr year using input\firm_it, keepusing(sales) keep(matched) nogen
merge m:1 jurnr year using input\fire_it, keepusing(capital wagebill expenditures) keep(matched) nogen

*FIRM LEVEL EXPORT CONTROLS
preserve
	use jurnr vare land year using input\export_ipdt if inrange(year,2002,2015) , clear
	destring vare, gen(cn8) force
	merge m:1 cn8 year using input\cn8_cn8plus_2002_2015, keepusing(cn8plus) keep(3) nogen
	keep jurnr land year
	duplicates drop
	gen dest=1
	collapse (sum) dest, by(jurnr year)
	save temp\temp, replace
restore
merge m:1 jurnr year using temp\temp, keep(1 3) keepusing(dest) nogen
erase temp\temp.dta
replace dest=0 if dest==.
gen exp=(dest>0)

*FIRM-PRODUCT LEVEL EXPORT CONTROLS
preserve
	use jurnr vare land year using input\export_ipdt if inrange(year,2002,2015) , clear
	destring vare, gen(cn8) force
	merge m:1 cn8 year using input\cn8_cn8plus_2002_2015, keepusing(cn8plus) keep(3) nogen
	rename cn8plus vnr
	keep jurnr vnr land year
	duplicates drop
	gen dest_p=1
	collapse (sum) dest_p, by(jurnr vnr year)
	save temp\temp, replace
restore
merge m:1 jurnr vnr year using temp\temp, keep(1 3) keepusing(dest_p) nogen
erase temp\temp.dta
replace dest_p=0 if dest_p==.
gen exp_p=(dest_p>0)	

*FIRM LEVEL EXPORT SUPPORT
preserve
	use input\promotion, clear
	collapse (max) treated tdcinitiative, by(jurnr_num year)

	reshape wide treated tdcinitiative , i(jurnr_num) j(year)
	reshape long	
	
	xtset jurnr_num year
	foreach v of varlist treated tdcinitiative {
		replace `v'=0 if `v'==.
		gen `v'_2=(`v'==1 | l.`v'==1)
		drop `v'
		rename `v'_2 `v'
	}
	
	save temp\temp, replace
restore	
merge m:1 jurnr_num year using temp\temp, keep(1 3) keepusing(treated tdcinitiative) nogen
erase temp\temp.dta

*FIRM-PRODUCT LEVEL EXPORT SUPPORT
preserve
	use input\promotion, clear
	collapse (max) treated tdcinitiative, by(jurnr_num land year)

	reshape wide treated tdcinitiative , i(jurnr_num land) j(year)
	reshape long	
	
	save temp\temp, replace
restore	
preserve
	use jurnr vare land year using input\export_ipdt if inrange(year,2002,2015) & land!="" , clear
	destring vare, gen(cn8) force
	merge m:1 cn8 year using input\cn8_cn8plus_2002_2015, keepusing(cn8plus) keep(3) nogen
	rename cn8plus vnr
	keep jurnr vnr land year
	duplicates drop	
	
	destring jurnr, gen(jurnr_num)
	merge m:1 jurnr_num land year using temp\temp, keepusing(treated tdcinitiative) keep(1 3) nogen
	erase temp\temp.dta
	foreach v of varlist treated tdcinitiative {
	    replace `v'=0 if `v'==.
	}
	
	collapse (max) treated_p=treated tdcinitiative_p=tdcinitiative , by(jurnr_num vnr year)
	reshape wide treated tdcinitiative , i(jurnr_num vnr) j(year)
	reshape long

	egen fp=group(jurnr_num vnr)
	xtset fp year
	foreach v of varlist treated_p tdcinitiative_p {
		replace `v'=0 if `v'==.
		gen `v'_2=(`v'==1 | l.`v'==1)
		drop `v'
		rename `v'_2 `v'
	}	
	
	save temp\temp, replace
restore
merge m:1 jurnr_num vnr year using temp\temp, keep(1 3) keepusing(treated_p tdcinitiative_p) nogen
erase temp\temp.dta

foreach v in treated treated_p tdcinitiative tdcinitiative_p {
    replace `v'=0 if `v'==.
}


*DEFLATION
merge m:1 db07 using input\industrykey, assert(2 3) keep(3) keepusing(g69) nogen
drop db07

gen expenditures_nom=expenditures

foreach y of numlist 2002/2015 {
	merge m:1 g69 year using input\MPI_`y', update keep(1 3 4) nogen
	merge m:1 g69 year using input\GFCF_`y', update keep(1 3 4) nogen
	replace expenditures=expenditures/MPI if year==`y'
	replace capital=capital/GFCF if year==`y'
}
	
merge m:1 year using input\CPI, assert(2 3) keep(3) nogen
replace wagebill=wagebill/CPI

drop MPI GFCF CPI g69


*ADJUST FIRM-LEVEL STUFF TO "PRODUCT COVERAGE"
foreach v of varlist sales wagebill capital expenditures expenditures_nom {
	replace `v'=`v'*share
	}
drop share


*HS SECTIONS
gen vnr0=floor(vnr6/1E4)

gen		HS=1 	if vnr0<6
replace HS=2 	if vnr0>=6 & vnr0<15
replace HS=3	if vnr0==15
replace HS=4 	if vnr0>=16 & vnr0<25
replace HS=5 	if vnr0>=25 & vnr0<28
replace HS=6 	if vnr0>=28 & vnr0<39
replace HS=7 	if vnr0>=39 & vnr0<41
replace HS=8 	if vnr0>=41 & vnr0<44
replace HS=9 	if vnr0>=44 & vnr0<47
replace HS=10	if vnr0>=47 & vnr0<50
replace HS=11 	if vnr0>=50 & vnr0<64
replace HS=12 	if vnr0>=64 & vnr0<68
replace HS=13 	if vnr0>=68 & vnr0<71
replace HS=14	if vnr0==71			 
replace HS=15 	if vnr0>=72 & vnr0<84
replace HS=16 	if vnr0>=84 & vnr0<86
replace HS=17 	if vnr0>=86 & vnr0<90
replace HS=18 	if vnr0>=90 & vnr0<93
replace HS=19	if vnr0==93          
replace HS=20	if vnr0>=94 & vnr0<97
replace HS=21 	if vnr0==97 | vnr0==98
drop vnr0

rename vnr6 HS6
gen HS4=floor(HS6/100)
gen HS3=floor(HS4/10)
gen HS2=floor(HS3/10)


*RESTRICTION
keep if sales!=. & sales>0 & wagebill!=. & wagebill>0 & capital!=. & capital>0 & expenditures!=. & expenditures>0


*SAVE
sort jurnr vnr mgd_tekst year
compress
save output\vars_ipt, replace